<template>
  <van-goods-action>
    <van-goods-action-icon
      :icon="isCol ? 'star' : 'star-o'"
      :text="isCol ? '已收藏' : '收藏'"
      :color="isCol ? '#ff5000' : ''"
      @click="colGoods"
    />
    <van-goods-action-icon
      icon="cart-o"
      text="购物车"
      :badge="token ? $store.state.cartNum + '' : ''"
      to="/shopping"
    />
    <van-goods-action-button type="danger" text="立即购买" @click="goBuy" />
    <van-goods-action-button type="warning" text="加入购物车" @click="showAdd" />
  </van-goods-action>
</template>

<script>
import { addCartData, GetCartNumData, login } from "@/request/api";
export default {
  props: ["info", "goods"],
  data() {
    return {
      total: 0,
      isCol: false
    };
  },
  computed: {
    token() {
      let a = localStorage.getItem("token");
      if (a) {
        return a;
      } else {
        return false;
      }
    }
  },
  methods: {
    // 点击收藏
    colGoods() {
      if (!this.token) {
        this.$router.push("/user");
        return this.$toast.fail("请先登录！");
      }
      if (!this.isCol) {
        this.isCol = true;
        this.$toast.success("收藏成功！");
      } else {
        this.isCol = false;
        this.$toast.fail("取消收藏！");
      }
    },

    // 显示加入购物车
    showAdd() {
      if (!localStorage.getItem("token")) {
        this.$toast.fail("请先登录！");
        this.$router.push("/user");
        return;
      }
      if (!this.$store.state.showAddCart) {
        this.$store.commit("changeShowAddCart", true);
      } else {
        this.addCart();
      }
    },

    // 点击按钮加入购物车
    async addCart() {
      const res = await addCartData({
        goodsId: this.info.goods_id,
        productId: this.info.id,
        number: this.$store.state.goodsNum
      });
      console.log(res, "aaaaaa");
      if (res.errno == 0) {
        this.$store.commit("changeCartNum", res.data.cartTotal.goodsCount);
        this.$toast.success("加入购物车成功");
        this.$store.commit("changeShowAddCart", false);
        this.$store.commit("changeGoodsNum", 1);
      } else {
        this.$toast.fail(res.errmsg);
      }
    },

    // 获取购物车数量
    async getCartNum() {
      if (!localStorage.getItem("token")) {
        return false;
      }
      const res = await GetCartNumData();
      this.total = res.data.cartTotal.goodsCount;
      this.$store.commit("changeCartNum", res.data.cartTotal.goodsCount);
    },

    // 立即购买
    goBuy() {
      if (!this.$store.state.showAddCart) {
        this.$store.commit("changeShowAddCart", true);
      } else {
        // this.$toast("该功能未开放");
        var arr = [];
        arr.push(this.goods);
        this.$router.push({
          name: "order",
          params: {
            productIds: arr
          }
        });
        this.$store.commit("changeShowAddCart", false);
      }
    }
  },
  created() {
    // ==========
    //  模拟token    ========待删除========
    // 判断用户登录，获取购物车数量
    if (localStorage.getItem("token")) {
      this.getCartNum();
    }
  }
};
</script>

<style lang="less" scoped>
.van-goods-action {
  z-index: 9;
}
</style>
